<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">ID</label>
            <div class="layui-input-block">
                <input type="text"  id="id" name="id" lay-verify="required"  value={{.id}} class="layui-input" readonly>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">软件名称</label>
            <div class="layui-input-block">
                <input type="text" name="softname" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">软件版本号</label>
            <div class="layui-input-block">
                <input type="text" name="softver" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">软件分类</label>
            <div class="layui-input-block">
                <select name="cfcation" lay-verify="required">
                    {{range $k,$v:=.cfcation}}
                        <option value={{$v.Id}}>{{$v.Cfname}}</option>
                    {{end}}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">云盘地址</label>
            <div class="layui-input-block">
                <input type="text" name="softaddr" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">云盘密码</label>
            <div class="layui-input-block">
                <input type="text" name="softpwd" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">软文标题</label>
            <div class="layui-input-block">
                <input type="text" name="softtitle" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">关键词</label>
            <div class="layui-input-block">
                <input type="text" name="softkeyword" lay-verify="required"  value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="softico" name="softico">
                        <p id="softicoimg" name="softicoimgsrc"></p>
                    </div>
                    <button type="button" class="layui-btn" id="softicobt">上传图片</button>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <textarea id="textedit" style="display: none;"></textarea>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','layedit','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        var layedit=layui.layedit;
        var upload = layui.upload;

        layedit.set({
            uploadImage: {
                url: '/admin/soft/edittext/img/upload' //接口url
                ,type: 'post' //默认post
            }
        });

        var editIndex =layedit.build('textedit');

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //图片上传
        var uploadInst = upload.render({
            elem: '#softicobt',
            url: '/admin/soft/img/upload', 
            size:200,
            data: {
                id: function(){
                    return $('#id').val();
                }
            },before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#softico').attr('src', result); //图片链接（base64）
                });
            },done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }else {
                    $("#softicoimg").text(res.data.src);
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#softicoimg');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.post(
                "/admin/soft/api/add",
                {
                    formtext:data.field,
                    imgsrc:$('#softicoimg').text(),
                    edittext:layedit.getContent(editIndex)
                },
                function (res) {
                    if (res.status) {
                        layer.msg(res.message, { time: 2000 });
                    } else {
                        layer.msg(res.message, { time: 2000 });
                    }
                }, 'json');

            // 关闭弹出层
            layer.close(parentIndex);

            return false;
        });

    });
</script>